/**
 * 获取学生选题数据模型
 */
module.exports = class Mytopic extends require('./model') {
    /**
     * 获取学生选题列表
     */
    static getListByUid(uid) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT a.id,a.title,a.uid FROM article a,`user` u WHERE (u.aid = a.id OR u.aid2 = a.id OR u.aid3 = a.id) AND u.uid = ?'
            this.query(sql, uid).then(results => {
                resolve(results)
            }).catch(err => {
                console.log(`获取学生选题列表失败：${err.message}`)
                reject(err)
            })
        })
    }
    /* 
        获取学生选的题目aid
    */
    static getInfoByUid(uid) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT aid,aid2,aid3 FROM `user` WHERE uid = ?'
            this.query(sql, uid).then(results => {
                resolve(results[0])
            }).catch(err => {
                console.log(`获取学生信息列表失败：${err.message}`)
                reject(err)
            })
        })
    }
    /* 
        学生选题
    */
    // static addAid(aid,aid2,aid3,uid) {
    //     return new Promise((resolve, reject) => {
    //         let sql = 'UPDATE `user` SET aid = ?, aid2 = ?, aid3 = ? WHERE uid = ?'
    //         this.query(sql, [aid,aid2,aid3,uid]).then(results => {
    //             resolve(results.affectedRows)
    //         }).catch(err => {
    //             console.log(`学生选题失败：${err.message}`)
    //             reject(err)
    //         })
    //     })
    // }
    /* 
        学生选题
    */
    static addAid(aid,aid2,aid3,uid) {
        return new Promise((resolve, reject) => {
            let sql = 'UPDATE `user` SET'
            sql += aid ? ` aid=${aid},` : ` aid=null,`
            sql += aid2 ? ` aid2=${aid2},` : ` aid2=null,`
            sql += aid3 ? ` aid3=${aid3} WHERE uid = ${uid}` : ` aid3=null WHERE uid = ${uid}`
            this.query(sql).then(results => {
                resolve(results.affectedRows)
            }).catch(err => {
                console.log(`学生选题失败：${err.message}`)
                reject(err)
            })
        })
    }
    /* 
        最终结果
    */
    static getres(uid) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT u.username,u.uid,a.id,a.title,t.tname,t.tphone FROM `user` u,article a, teacher t WHERE a.uid = u.uid AND a.tid = t.id AND u.uid = ?'
            this.query(sql, uid).then(results => {
                resolve(results[0])
            }).catch(err => {
                console.log(`学生选题失败：${err.message}`)
                reject(err)
            })
        })
    }

}